package com.yonyou.pmclouds.contacttype.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.IllegalRequestArgException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.contacttype.entity.ContactTypeVO;
import com.yonyou.pmclouds.contacttype.service.rmiitf.ContactTypeRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Slf4j
@Controller
@RequestMapping(value = "/contacttype")
public class ContactTypeController {

    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ContactTypeRemoteService contactTypeRemoteService;

    @RequestMapping(value = "/queryContactType")
    @ResponseBody
    public List<ContactTypeVO> queryContactType(HttpServletResponse response) {
        List<ContactTypeVO> resultList = contactTypeRemoteService.selectByPkGroup(RuntimeEnvironment.getTenantId());
        return resultList;
    }
    @RequestMapping(value = "/addContactType")
    @ResponseBody
    public ContactTypeVO addContactType(@Validated @RequestBody ContactTypeVO contactTypeVO, BindingResult errors) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        contactTypeVO = contactTypeRemoteService.addContactType(contactTypeVO);
        return contactTypeVO;
    }
    @RequestMapping(value = "/deleteContactType")
    @ResponseBody
    public String deleteContactType(@RequestBody ContactTypeVO contactTypeVO) throws BusinessException {
        contactTypeRemoteService.deleteContactType(contactTypeVO.getPk_contacttype(), contactTypeVO.getChangeFlag());
        return "删除成功！";
    }

    @RequestMapping(value = "/updateContactType")
    @ResponseBody
    public ContactTypeVO updateContactType(@Validated @RequestBody ContactTypeVO contactTypeVO, BindingResult errors) throws BusinessException {
        if (errors.hasErrors()) {
            throw new IllegalRequestArgException();
        }
        contactTypeVO = contactTypeRemoteService.updateContactType(contactTypeVO);
        return contactTypeVO;
    }

}
